﻿
apolloRelation = function (containerLink, containerText, containerImageAdd, containerImageDel, containerHiddenId, containerHiddenDisplayName, viewId, editUrl) {
    this.domContainerLink = $(("#" + containerLink));
    this.domContainerLinkHolder = $(("#" + containerLink + "_holder"));
    this.domContainerText = $(("#" + containerText));
    this.domContainerImageAdd = $(("#" + containerImageAdd));
    this.domContainerImageDel = $(("#" + containerImageDel));
    this.domContainerHiddenId = $(("#" + containerHiddenId));
    this.domContainerHiddenDisplayName = $(("#" + containerHiddenDisplayName));
    this.viewId = viewId;
    this.editUrl = editUrl;

    this.load();
}

apolloRelation.prototype.showHyperLink = function () {
    this.domContainerLinkHolder.show();
    this.domContainerText.hide();
}

apolloRelation.prototype.showTextBox = function () {
    this.domContainerText.show();
    this.domContainerText.val(this.domContainerLink.html());
    this.domContainerLinkHolder.hide();
    this.domContainerText.select();
}

apolloRelation.prototype.load = function () {
    var currentValue = this.domContainerHiddenId.val();

    this.showHyperLink();

    if (currentValue) {
        var currentEditUrl = this.editUrl.replace("{ObjectId}", currentValue);

        this.domContainerLink.attr("href", "javascript:ModalOpen('" + currentEditUrl + "', false)");
        this.domContainerLink.attr("title", this.domContainerHiddenDisplayName.val());
        this.domContainerLink.html(this.domContainerHiddenDisplayName.val());
        this.domContainerText.val(this.domContainerHiddenDisplayName.val());

    } else {
        if (this.domContainerImageDel)
            this.domContainerImageDel.hide();

        this.domContainerLink.html("");
        this.domContainerLink.attr("title", "");
    }

    if (this.domContainerText.hasClass("property_relation_disabled")) {
        this.domContainerLinkHolder.removeClass("property_relation_holder");
        this.domContainerLinkHolder.addClass("property_relation_holder_disabled");
        return;
    }

    var that = this;

    this.domContainerLinkHolder.click(function () { that.showTextBox(); });

    this.domContainerText.keypress(function (event) {
        if (event.keyCode == 27)
            that.showHyperLink();
    });

    var sourceUrl = "/Autocomplete.ashx?ViewId=" + this.viewId;
    this.domContainerText.autocomplete({
        source: sourceUrl,
        select: function (event, ui) {

            that.domContainerLink.html(ui.item.label);
            that.domContainerLink.attr("title", ui.item.label);
            var currentEditUrl = that.editUrl.replace("{ObjectId}", ui.item.value);
            that.domContainerLink.attr("href", "javascript:ModalOpen('" + currentEditUrl + "', false)");
            that.domContainerHiddenDisplayName.val(ui.item.label);
            that.domContainerText.val(ui.item.label);
            that.domContainerHiddenId.val(ui.item.value);
            that.domContainerHiddenDisplayName.val(ui.item.label);
            that.domContainerImageDel.show();

            that.showHyperLink();
        }
    });

    if (this.domContainerImageAdd != null)
        this.domContainerImageAdd.click(function () {
            var args = { 'callback': function (valFromModal) {

                if (valFromModal) {

                    that.domContainerImageDel.show();

                    that.domContainerHiddenId.val(valFromModal.Id);

                    that.domContainerLink.html("<img src='/css/apollo/loading16x16.gif' border='0' />");

                    var url = "/JSONEntity.ashx?ViewId=" + that.viewId + "&ObjectId=" + valFromModal.Id;

                    jQuery.ajax({
                        url: url,
                        dataType: "text",
                        success: function (jsonData) {
                            var jsonObject = jQuery.parseJSON(jsonData);

                            that.domContainerLink.html(jsonObject.DisplayName);
                            that.domContainerLink.attr("title", jsonObject.DisplayName);
                            that.domContainerHiddenDisplayName.val(jsonObject.DisplayName);
                            that.domContainerText.val(jsonObject.DisplayName);
                        }
                    });

                    var currentEditUrl = that.editUrl.replace("{ObjectId}", valFromModal.Id);
                    that.domContainerLink.attr("href", "javascript:ModalOpen('" + currentEditUrl + "', false)");

                }
            }
            };

            var scr_w = Math.round(screen.width * 0.7);
            var scr_h = Math.round(screen.height * 0.7);
            var scr_t = Math.round((screen.width - scr_w) / 2);
            var scr_l = Math.round((screen.height - scr_h) / 2);

            var feats = 'dialogHeight=' + scr_h + 'px; dialogWidth=' + scr_w + 'px; dialogTop=' + scr_t + 'px; dialogLeft=' + scr_l + 'px;  resizable=yes';

            var modalUrl = '/QuickView.aspx?t=1&ViewId=' + that.viewId;

            window.showModalDialog(modalUrl, args, feats);

            return false;
        });

    if (this.domContainerImageDel)
        this.domContainerImageDel.click(function () {
            that.domContainerLink.text("");
            that.domContainerLink.attr("href", "");
            that.domContainerLink.html("");
            that.domContainerText.val("");
            that.domContainerHiddenId.val("");
            that.domContainerHiddenDisplayName.val("");
            that.domContainerImageDel.hide();

            return false;
        });
}
